﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Controls;

namespace JarvisProject1
{
    public class Item : System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        //--------------------------------------------
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
        //--------------------------------------------
        // Объявление основных свойств
        //--------------------------------------------
        private string _Title = string.Empty;
        public string Title
        {
            get
            {
                return this._Title;
            }

            set
            {
                if (this._Title != value)
                {
                    this._Title = value;
                    this.OnPropertyChanged("Title");
                }
            }
        }

        private string _Subtitle = string.Empty;
        public string Subtitle
        {
            get
            {
                return this._Subtitle;
            }

            set
            {
                if (this._Subtitle != value)
                {
                    this._Subtitle = value;
                    this.OnPropertyChanged("Subtitle");
                }
            }
        }

        private ImageSource _Image = null;
        public ImageSource Image
        {
            get
            {
                return this._Image;
            }

            set
            {
                if (this._Image != value)
                {
                    this._Image = value;
                    this.OnPropertyChanged("Image");
                }
            }
        }

        public void SetImage(Uri baseUri, String path)
        {
            Image = new BitmapImage(new Uri(baseUri, path));
        }

        private string _Link = string.Empty;
        public string Link
        {
            get
            {
                return this._Link;
            }

            set
            {
                if (this._Link != value)
                {
                    this._Link = value;
                    this.OnPropertyChanged("Link");
                }
            }
        }

        private string _Category = string.Empty;
        public string Category
        {
            get
            {
                return this._Category;
            }

            set
            {
                if (this._Category != value)
                {
                    this._Category = value;
                    this.OnPropertyChanged("Category");
                }
            }
        }

        private string _Description = string.Empty;
        public string Description
        {
            get
            {
                return this._Description;
            }

            set
            {
                if (this._Description != value)
                {
                    this._Description = value;
                    this.OnPropertyChanged("Description");
                }
            }
        }

        private string _Content = string.Empty;
        public string Content
        {
            get
            {
                return this._Content;
            }

            set
            {
                if (this._Content != value)
                {
                    this._Content = value;
                    this.OnPropertyChanged("Content");
                }
            }
        }
        //--------------------------------------------
    }
    //--------------------------------------------
    public class GroupInfoList <T> : List<object>
    {
        public object Key { get; set; }
        public new IEnumerator <object> GetEnumerator()
        {
            return (System.Collections.Generic.IEnumerator <object>)base.GetEnumerator();
        }
    }
    //--------------------------------------------
    // Создание коллекции рубрик
    //--------------------------------------------
    public class StoreDataRubric
    {
        public StoreDataRubric()
        {
            Item item;
            MainPage obj = new MainPage();
            Uri _baseUri = new Uri("ms-appx:///");
            String LONG_LOREM_IPSUM = String.Empty;
         
            for (int i = 0; i < obj._object[0]._Rubrics.Count; i++)
            {
                item = new Item();
                item.Title = obj._object[0]._Rubrics[i].RubrikName;
                item.SetImage(_baseUri, obj._object[0]._Rubrics[i].Rubrik_Image_Adress);
                CollectionRubric.Add(item);
            }
        }

        public ItemCollection _CollectionRubric = new ItemCollection();

        public ItemCollection CollectionRubric
        {
            get
            {
                return this._CollectionRubric;
            }
        }

        internal List <GroupInfoList <object>> GetGroupsByLetter()
        {
            List<GroupInfoList<object>> groups = new List<GroupInfoList<object>>();

            var query = from item in CollectionRubric
                        orderby ((Item)item).Title
                        group item by ((Item)item).Title[0] into g
                        select new { GroupName = g.Key, Items = g };
            foreach (var g in query)
            {
                GroupInfoList <object> info = new GroupInfoList <object>();
                info.Key = g.GroupName;
                foreach (var item in g.Items)
                {
                    info.Add(item);
                }
                groups.Add(info);
            }
            return groups;
        }
    }

    //--------------------------------------------
    // Создание коллекции сообщений по выбранному
    // индексу
    //--------------------------------------------
    public class StoreDataMessage
    {
        public StoreDataMessage(int RubricIndex)
        {
            Item item;
            MainPage obj = new MainPage();
            Uri _baseUri = new Uri("ms-appx:///");
            String LONG_LOREM_IPSUM = String.Empty;
            
            for (int i = 0; i < obj._object[0]._Rubrics[RubricIndex].Messsages.Count; i++)
            {
                item = new Item();
                item.Title = obj._object[0]._Rubrics[RubricIndex].Messsages[i].Name_Message;
                item.SetImage(_baseUri, obj._object[0]._Rubrics[RubricIndex].Messsages[i].ImageAdress);
                CollectionMessage.Add(item);
            }
        }

        public ItemCollection _CollectionMessage = new ItemCollection();

        public ItemCollection CollectionMessage
        {
            get
            {
                return this._CollectionMessage;
            }
        }

        internal List <GroupInfoList<object>> GetGroupsByLetter()
        {
            List <GroupInfoList <object>> groups = new List<GroupInfoList<object>>();

            var query = from item in CollectionMessage
                        orderby ((Item)item).Title
                        group item by ((Item)item).Title[0] into g
                        select new { GroupName = g.Key, Items = g };
            foreach (var g in query)
            {
                GroupInfoList <object> info = new GroupInfoList <object>();
                info.Key = g.GroupName;
                foreach (var item in g.Items)
                {
                    info.Add(item);
                }
                groups.Add(info);
            }
            return groups;
        }
    }

    //--------------------------------------------
    public class ItemCollection : IEnumerable <Object>
    {
        private System.Collections.ObjectModel.ObservableCollection <Item> itemCollection = new System.Collections.ObjectModel.ObservableCollection<Item>();
        public IEnumerator<Object> GetEnumerator()
        {
            return itemCollection.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        public void Add(Item item)
        {
            itemCollection.Add(item);
        }
    }
}

